package com.tengju.bff.interfaces.manage.member.resource.assembler;

import com.google.common.collect.Lists;
import com.tengju.bff.interfaces.manage.member.resource.vo.AddResourceForm;
import com.tengju.bff.interfaces.manage.member.resource.vo.PageResourceVO;
import com.tengju.bff.interfaces.manage.member.resource.vo.ResourceVO;
import com.tengju.support.domain.model.resource.JumpAction;
import com.tengju.support.domain.model.resource.Position;
import com.tengju.support.domain.model.resource.Resource;
import com.tengju.support.domain.model.resource.ResourceOrientation;
import com.tengju.user.application.archives.dto.PageQueryDTO;
import com.tengju.user.domain.model.user.Staff;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

public class ResourceAssembler {

    public static Resource voToModel(AddResourceForm form, Staff staff) {
        List<ResourceOrientation> orientations  = form.getUserOrientation().stream()
                .map(ResourceOrientation::fromVal).collect(Collectors.toList());
        return new Resource(
                null,JumpAction.WEB_VIEW, form.getImgUrl(),
                form.getTitle(), form.getJumpUrl(),
                staff.getName(), Position.fromVal(form.getPosition()),
                form.getBeginTime(),form.getEndTime(), orientations,
                form.getPriority(), staff.getName(), LocalDateTime.now()
        );
    }

    public static PageResourceVO modelToVO(List<Resource> resourceList, PageQueryDTO pageQueryDTO, Integer total) {

        List<ResourceVO> resourceVOList = Lists.newArrayList();
        PageResourceVO pageResourceVO = new PageResourceVO();
        pageResourceVO.setPageIndex(pageQueryDTO.getPageIndex());
        pageResourceVO.setPageSize(pageQueryDTO.getPageSize());
        pageResourceVO.setTotal(total);

        if (CollectionUtils.isEmpty(resourceList)) {
            return pageResourceVO;
        }

        for (Resource resource : resourceList) {
            ResourceVO resourceVO = new ResourceVO();
            resourceVO.setResourceId(resource.getResourceId().getValue());
            resourceVO.setImgUrl(resource.getImgUrl());
            resourceVO.setTitle(resource.getTitle());
            resourceVO.setJumpUrl(resource.getJumpUrl());
            resourceVO.setBeginTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(resource.getBeginTime()));
            resourceVO.setEndTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(resource.getEndTime()));
            resourceVO.setPosition(resource.getPosition().getVal());
            resourceVO.setPriority(resource.getPriority());
            resourceVO.setUserOrientation(resource.getUserOrientation().stream().map(ResourceOrientation::getValue).collect(Collectors.toList()));
            resourceVO.setCreateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(resource.getCreateTime()));
            resourceVO.setCreateBy(resource.getCreateBy());
            resourceVOList.add(resourceVO);
        }
        pageResourceVO.setResourceVOList(resourceVOList);
        return pageResourceVO;
    }
}
